package com.ninetaildemonfox.zdl.socialproject.utils.dialog;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hyphenate.easeui.utils.map.BitmapViewUtils;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.ShareDialogContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.ShareDialogPresenter;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * @author NineTailDemonFox
 * @date 2019/7/24 11:21
 * 功能描述： 分享界面
 * 联系方式：1037438704@qq.com
 */

public class ShareDialogActivity extends BaseActivity implements ShareDialogContract, PlatformActionListener {
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_invite_code)
    TextView textInviteCode;
    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.text_qq)
    TextView textQQ;
    @BindView(R.id.text_wx)
    TextView textWx;
    @BindView(R.id.text_friend)
    TextView textFriend;
    @BindView(R.id.text_save_it_locally)
    TextView text_save_it_locally;
    @BindView(R.id.scro)
    ScrollView scro;
    private ShareDialogPresenter presenter;
    private String url;
    private Platform.ShareParams params;
    private Platform share;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_share_dialog;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        presenter = new ShareDialogPresenter(this, me);
        presenter.getInviteNow(token);
    }

    @OnClick({R.id.text_center, R.id.text_qq, R.id.text_wx, R.id.text_friend, R.id.text_save_it_locally})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_center:
                finish();
                break;
            case R.id.text_qq:
                share = ShareSDK.getPlatform(QQ.NAME);
                share.setPlatformActionListener(this);
                share.share(params);
                break;
            case R.id.text_wx:
                share = ShareSDK.getPlatform(Wechat.NAME);
                share.setPlatformActionListener(this);
                share.share(params);
                break;
            case R.id.text_friend:
                share = ShareSDK.getPlatform(WechatMoments.NAME);
                share.setPlatformActionListener(this);
                share.share(params);
                break;
            case R.id.text_save_it_locally:
                Bitmap bitmapByView = BitmapViewUtils.getBitmapByView(scro);
                String s = BitmapViewUtils.setBitmap(me, bitmapByView);
                ToastUtil.show("已保存至：" + s);
                break;
            default:
        }
    }

    @Override
    public void Success(Map<String, String> data) {
        String invite_code = data.get("invite_code");
        textInviteCode.setText(invite_code);
        Glide.with(me).load(data.get("qr_code")).into(imageView);
        if (!TextUtils.isEmpty(data.get("qr_url"))) {
            params = new Platform.ShareParams();
            Bitmap logo = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_logo);
            params.setShareType(Platform.SHARE_WEBPAGE);
            params.setImageData(logo);
            params.setTitle("最佳拍档");
            params.setText("看电影、玩游戏、去旅游...颜值最高的附近约会平台回复");
            params.setUrl(data.get("qr_url"));
        }
    }

    @Override
    public void Failed(String msg) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        ToastUtil.show("分享成功");
        finish();
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        ToastUtil.show("分享失败");
        finish();
    }

    @Override
    public void onCancel(Platform platform, int i) {
        ToastUtil.show("取消分享");
        finish();
    }
}
